package app.util;

import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import framework.system.FrontEnd;

public class DHTConnector {
	
	private static FrontEnd frontEnd;
	
	public static void insert(String key, String value){
		try {
			frontEnd.put(key.toLowerCase(), value);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static String[] get(String key){
		try {
			return frontEnd.get(key.toLowerCase());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static void remove(String key){
		try {
			frontEnd.remove(key.toLowerCase());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static boolean connectToDHT(String hostName){
		String port = Integer.toString(Registry.REGISTRY_PORT);
		try{
			Registry registry = LocateRegistry.getRegistry(hostName, Registry.REGISTRY_PORT);
			frontEnd = (FrontEnd) registry.lookup("rmi://" + hostName + ":" + port + "/" + FrontEnd.SERVICENAME);
			return true;
		} catch (Exception e){
			//e.printStackTrace();
			return false;
		}
	}

}
